package com.atguigu.hightest;

import com.atguigu.bean.UserBehavior;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class Flink02_PV_01 {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);

        DataStreamSource<String> ds = env.readTextFile("input/UserBehavior.csv");

        ds.map(new MapFunction<String, UserBehavior>() {
            @Override
            public UserBehavior map(String value) throws Exception {
                String[] datas = value.split(",");
                return new UserBehavior(
                        Long.valueOf(datas[0]),
                        Long.valueOf(datas[1]),
                        Integer.valueOf(datas[2]),
                        datas[3],
                        Long.valueOf(datas[4]) * 1000
                );
            }
        })
                //加水印
        .assignTimestampsAndWatermarks(WatermarkStrategy
                //设置最大乱序时间
                .<UserBehavior>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                //设置和水印的比较值
                .withTimestampAssigner(new SerializableTimestampAssigner<UserBehavior>() {
                    @Override
                    public long extractTimestamp(UserBehavior element, long recordTimestamp) {
                        return element.getTimestamp();
                    }
                })
        )
                //筛选元素
        .filter(new FilterFunction<UserBehavior>() {
            @Override
            public boolean filter(UserBehavior value) throws Exception {
                return "pv".equals(value.getBehavior());
            }
        })
                //keyby
        .keyBy(UserBehavior::getBehavior)
                //开窗
        .window(TumblingEventTimeWindows.of(Time.minutes(30)))
                //处理
        .process(new ProcessWindowFunction<UserBehavior, String, String, TimeWindow>() {

            private AggregatingState<UserBehavior, Long> accState;

            //使用状态来对值进行聚合
            @Override
            public void open(Configuration parameters) throws Exception {
                accState = getRuntimeContext().getAggregatingState(new AggregatingStateDescriptor<>("accState"
                        , new AggregateFunction<UserBehavior, Long, Long>() {
                    @Override
                    public Long createAccumulator() {
                        return 0L;
                    }

                    @Override
                    public Long add(UserBehavior value, Long accumulator) {
                        return accumulator + 1;
                    }

                    @Override
                    public Long getResult(Long accumulator) {
                        return accumulator;
                    }

                    @Override
                    public Long merge(Long a, Long b) {
                        return null;
                    }
                }, Long.class));
            }

            @Override
            public void process(String key,
                                Context context,
                                Iterable<UserBehavior> elements,
                                Collector<String> out) throws Exception {
                //在每一个窗口新开的时候清理一下上一个窗口的状态值
                accState.clear();
                //将值放入状态中
                for (UserBehavior element : elements) {
                    accState.add(element);
                }
                //输出结果
                out.collect(context.window()+" "+accState.get());


            }
        })
                .print();
                
                


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
